<?php


namespace api\controllers;


use common\helpers\commonApi;
use common\models\ActivityEnter;
use common\models\Order;
use common\models\System;
use common\models\VipLog;
use EasyWeChat\Factory;
use yii\web\Controller;

class PayNotifyController extends Controller
{
    public function actionActivity()
    {
        $system = System::getModel();
        $config = [
            'app_id'             => $system->a_appid,
            'mch_id'             => $system->mch_id,
            'key'                => $system->pay_key,
            'notify_url'         => \Yii::$app->request->hostInfo.'/pay-notify/activity',
        ];
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            $trans = \Yii::$app->db->beginTransaction();
            try {
                // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
                $order = ActivityEnter::find()->where(['orderid'=>$message['out_trade_no']])->one();
                if (!$order || $order->status!==0) {
                    $trans->commit();
                    return true;
                }
                if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态，不代表支付状态
                    // 用户是否支付成功
                    if ($message['result_code'] === 'SUCCESS') {
                        $order->callback_time = date('Y-m-d H:i:s'); // 更新支付时间为当前时间
                        $order->status = 1;
                    } elseif ($message['result_code'] === 'FAIL') {
                        $order->status = 2;
                    }
                } else {
                    $trans->commit();
                    return $fail('通信失败，请稍后再通知我');
                }
                $order->callback_info = json_encode($message);
                if ($order->save()!==false){
                    $trans->commit();
                    return true; // 返回处理完成
                }
                $trans->rollBack();
                return true; // 返回处理完成
            }catch (\Exception $e){
                $trans->rollBack();
                return true;
            }
        });
        $response->send();
    }

    /**
     * 微信充值回调
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function actionWxCard()
    {
        $system = System::getModel();
        if (!$system->mch_id||!$system->pay_key|| !$system->a_appid) {
            return commonApi::message('请配置微信支付参数！', false);
        }
        $config = [
            'app_id' => $system->a_appid,
            'mch_id' => $system->mch_id,
            'key' => $system->pay_key,
        ];
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function ($message, $fail){
            $recharge = new VipLog();
            $res = $recharge->callback($message);
            if (!$res){
                return $fail('通信失败，请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }

    public function actionOrder()
    {
        $system = System::getModel();
        if (!$system->mch_id||!$system->pay_key|| !$system->a_appid) {
            return commonApi::message('请配置微信支付参数！', false);
        }
        $config = [
            'app_id' => $system->a_appid,
            'mch_id' => $system->mch_id,
            'key' => $system->pay_key,
        ];
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function ($message, $fail){
            $recharge = new Order();
            $res = $recharge->payCallback($message);
            if (!$res){
                return $fail('通信失败，请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }
}